﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using API;
using mb;

namespace libmp
{

    public class DataStore
    {
        static ILog logger = App.logger;
        public static DataStore store;
        public static void Parse(string strInfo = null)
        {
            if (strInfo == null)
            {
                strInfo = File.ReadAllText(App.HotStorageRoot + "/res/data.json");
            }
            store = LitJson.JsonMapper.ToObject<DataStore>(strInfo);

            if (store == null)
            {
                App.logger.Log("parse data failed");
                store = new DataStore();
            }
            else
            {
                App.logger.Log("video", store.videoCategory.Count, store.video.Count);
                App.logger.Log("setting", store.setting);
                foreach (var item in store.video)
                {
                    store.videoMap[item.vid] = item;
                }
            }

            var so = mb.ServiceLocator.Instance.Get<IStartOption>();
            var str = so.GetStringValue("testVideoList");
            if (str != null)
            {
                var testVideoList = LitJson.JsonMapper.ToObject(str);
                for (int i = 0; i < testVideoList.Count; i++)
                {
                    var vi = store.video[i];
                    vi.vpath = (string) testVideoList[i]["vpath"];
                    vi.vkey = (string) testVideoList[i]["vkey"];
                }
            }
        }

        //-///////////////////////////////////////////////////////////////
        public class VideoCategory
        {
            public int vcateid;
            public string catename;
            public int ordercol;
            public int roles;
            public string catepic;
        }

        public class VideoItem
        {
            public int vid;
            public int vcateid;
            public string vname;
            public string vpic;
            public string vpath;
            public string vkey;
            public int views;
            public int praises;
            public int favs;
            public int forwords;
        }

        public class DomainItem
        {
            public string domain;
        }

        public class Setting
        {
            public int firstnum;
            public int viewnum;
            public int tuijiannum;
            public string viewpath;
            public string picpath;

            public override string ToString()
            {
                string s = LitJson.JsonMapper.ToJson(this);
                return s;
            }
        }

        public delegate void OnChangedType(DataStore store);
        public OnChangedType OnChanged;

        public List<VideoCategory> videoCategory;
        public List<VideoItem> recommendVideo;
        public List<VideoItem> video;
        public List<DomainItem> prodomain;
        public List<DomainItem> noticedomain;
        public Setting setting;

        public Dictionary<int, VideoItem> videoMap = new Dictionary<int, VideoItem>();

        public Mb.OnlineConfig onlineConfig;

        public DataStore()
        {
            OnChanged += delegate { };
        }

        public List<VideoItem> GetRecommendVideoList()
        {
            return video.Take(setting.firstnum).ToList();

        }

        public List<VideoCategory> GetVideoCategory()
        {
            return videoCategory;
        }

        public List<VideoItem> GetAllVideo()
        {
            return video;
        }

        //根据cateid获取所有的视频
        public List<VideoItem> GetVideoForCateID(int cateID)
        {
            var getVideoForCateID = new List<VideoItem> { };
            for (int i = 0; i < video.Count; i++)
            {
                if (video[i].vcateid == cateID)
                {
                    getVideoForCateID.Add(video[i]);
                }
            }
            return getVideoForCateID;
        }

        public List<VideoItem> GetVideoForUserID(int userId)
        {
            var getVideoForCateID = new List<VideoItem> { };
            for (int i = 0; i < video.Count; i++)
            {
                if (video[i].vcateid == userId)
                {
                    getVideoForCateID.Add(video[i]);
                }
            }
            return getVideoForCateID;
        }

        public List<VideoItem> GetHistoryVideos(int userId)
        {
            var getVideoForCateID = new List<VideoItem> { };
            for (int i = 0; i < video.Count; i++)
            {
                if (video[i].vcateid == userId)
                {
                    getVideoForCateID.Add(video[i]);
                }
            }
            return getVideoForCateID;
        }

        //获取推荐视频，现设定为同cateid的前八个视频
        public List<VideoItem> GetRecommendVideo(int cateID, int vID)
        {
            return GetAllVideo().Take(48).ToList();
        }

        public VideoItem GetVideoItemById(int id) => videoMap[id];

        public async void PullOnlineConfig()
        {
            try
            {
                var res = await gRPC.Instance.ServiceClient.PullOnlineConfigAsync(new Mb.Empty());
                logger.Log("PullOnlineConfig", res.AdImages.Count);
                onlineConfig = res;
                OnChanged(this);
            }
            catch (Exception e)
            {
                logger.Log("PullOnlineConfig fail", e.Message);
            }
        }
    }

}